<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class IndexController extends CommonController {

    public function indexAction(){
        $model=M();

        $db_version = $model->query('select version()');
        $this->assign('db_version',$db_version);

        $php_version = phpversion();
        $this->assign('php_version',$php_version);

        $os = php_uname('s');
        $this->assign('os',$os);

        // 问题总数
        $sql = "select count(*) as question from question";
        $question = $model->query($sql);
        $this->assign('question',$question);

        // 等待回答的问题
        $sql = "select count(*) as unanswered from question where reply=0";
        $unanswered = $model->query($sql);
        $this->assign('unanswered',$unanswered);

        // 回答总数
        $reply = $question['0']['question'] - $unanswered['0']['unanswered'];
        $this->assign('reply',$reply);

        // 会员总数
        $sql = "select count(*) as num from user where gid=2";
        $userInfo = $model->query($sql);
        $this->assign('userInfo',$userInfo);

        // 黑名单总数
        $sql = "select count(*) as num from user where gid=3";
        $user = $model->query($sql);
        $this->assign('user',$user);

//        $sql = " select t1.*,t2.* from question t1 left join category t2 on t1.cid=t2.id and yearweek(from_unixtime(t1.create_time,'%Y-%m-%d')) = yearweek(now());";
//
//        $sql = "select t1.*,t2.category_name from question t1 inner join category t2 on t1.cid=t2.id and from_unixtime(t1.create_time,'%Y-%m') = date_format(now(),'%Y-%m')";
//
//        $sql = "select count(*),t2.category_name from question t1 left join category t2 on t1.cid=t2.id group by t2.category_name";

//        select count(*) cnt,if((unix_timestamp(now())-t1.create_time)<(3600*24*7),1,if((unix_timestamp(now())-t1.create_time)<(3600*24*30),2,3)) num,t2.category_name from question t1 LEFT JOIN category t2 on t1.cid=t2.id group by num,cid;

        $this->display('index');
    }

    public function questionAction()
    {
        $Model = M();

        $sql = "select count(*) num,from_unixtime(create_time,'%m-%d') as day from question where create_time>unix_timestamp(date_sub(curdate(), interval 30 day)) and create_time< unix_timestamp(curdate()) group by day order by day asc limit 7";

        $res = $Model->query($sql);

        $res = json_encode($res);

        echo $res;
    }


    public function registerAction()
    {
        $Model = M();

        $sql = "select count(*) num,from_unixtime(register_time,'%m-%d') as day from user where register_time>unix_timestamp(date_sub(curdate(), interval 30 day)) and register_time< unix_timestamp(curdate()) group by day order by day asc limit 7";

        $res = $Model->query($sql);

        $res = json_encode($res);

        echo $res;
    }

    public function answerAction()
    {
        $Model = M();

        $sql = "select count(*) num,from_unixtime(create_time,'%m-%d') as day from answer where create_time>unix_timestamp(date_sub(curdate(), interval 30 day)) and create_time< unix_timestamp(curdate()) group by day order by day asc limit 7";

        $res = $Model->query($sql);

        $res = json_encode($res);

        echo $res;
    }
}